<?php 

include"../function/function.php";


创建数组,echo只能输出标量类型，但数组是复合类型
$arr=[];
$arr[0]="天河";
$arr[1]="白云";
var_dump($arr);

$mark=[33,56,88,90,46,83];
var_dump($mark);


$city=array("北京","上海","广州","深圳");
var_dump($city);

$name=array();
$name[0]="张三";
var_dump($name);



// 三种数组类型
// 索引数组
$car[0]="法拉利";
$car[1]="劳斯莱斯";
$car[2]="宾利";

// 关联数组，带有指定键的数组
$age["张三"]="18";
$age["李四"]="22";
$age["陈五"]="29";
var_dump($age["李四"]);

$xinxi=array("name"=>"mayun","age"=>"50","chandi"=>"china");
var_dump($xinxi);


// 多维数组
$language=["英语","俄语","汉语",["粤语","四川话","湖南话"],"葡萄语"];
var_dump($language[3][0]);

$tall=array(123,159,165,array(175,180),178,190);
var_dump($tall[3][0]);






// foreach循环
// 将$arr的值赋值给$a变量,然后输出这个值，再循环遍历
$arr=array(1,2,3,4,5);
foreach ($arr as $a ) {
	echo $a.'<br>';
}


// $key是键，$value是值
$xinxi=array("name"=>"mayun","age"=>"50","chandi"=>"china");
foreach ($xinxi as $key => $value) {
	echo $key;
	echo "&nbsp";
	echo $value;
	echo "<br>";
}

// 比较数组，返回$bi1的某一些值，这些值是$bi2没有的，不改变原来的数组
$bi1=array("广州","上海","深圳");
$bi2=array("佛山","上海","杭州","成都");
var_dump(array_diff($bi1, $bi2)) ;
var_dump($bi1);

$bi3=array("name"=>"tom","age"=>"18","sex"=>"男");
$bi4=array("name"=>"lili","age"=>"18","sex"=>"女","tall"=>"180");
var_dump(array_diff($bi3, $bi4)) ;
var_dump($bi3);


// 返回数组中所有的键名。
$bi5=array("name"=>"tom","age"=>"18","sex"=>"男");
var_dump(array_keys($bi1));

// 把一个或多个数组合并为一个数组
$b11=array("广州","上海","深圳");
$b22=array("佛山","上海","杭州","成都");
var_dump(array_merge($b11,$b22));





// 删除数组的最后一个元素,改变原来的数组
$bbb=array("name"=>"tom","age"=>"18","sex"=>"男");
var_dump(array_pop($bbb));
var_dump($bbb);


// 将一个或多个元素插入数组的末尾,改变原来的数组
$c=array("广州","上海","深圳");
var_dump(array_push($c, "北京","贵阳"));
var_dump($c);



// 返回数组中一个或多个随机的键
$cc=array("name"=>"tom","age"=>"18","sex"=>"男","tall"=>"180","from"=>"china");
var_dump(array_rand($cc,2));


// 使用后面数组的值替换第一个数组的值,生成一个新的数组，不改变原来的数组
$ccc=array("佛山","上海","杭州","成都");
$ccc1=array("天河","越秀","白云","荔湾","萝岗","南沙","海珠");
var_dump(array_replace($ccc,$ccc1));
var_dump($ccc);


// 删除数组中首个元素，并返回被删除元素的值,改变原来的数组
$d=array("佛山","上海","杭州","成都");
var_dump(array_shift($d));
var_dump($d);


// 返回数组中被选定的部分array_slice(array,start,length,true or false)
$dd=array("name"=>"tom","age"=>"18","sex"=>"男","tall"=>"180","from"=>"china");
var_dump(array_slice($dd, 0,-1,false));




// 删除并替换数组中指定的元素,改变原来的数组
$ddd=[1,2,3,4,5,9,39,4,9,3];
$ddd1=[2,2,2,2,2,2,2,2,2,2];
var_dump(array_splice($ddd, 0,4,$ddd1));
var_dump($ddd);

// 删除数组中的重复值,新建一个数组，不改变原来的数组
$e=[100,200,300,400,500,300,300,400,700];
var_dump(array_unique($e));
var_dump($e);


// 在数组开头插入一个或多个元素,改变原来的数组
$ee=array("广州","深圳");
var_dump(array_unshift($ee, "汕头","香港","赤壁"));
var_dump($ee);

 // 对关联数组按照键值进行降序排序。改变原来的数组 
$eee=array("tim"=>"46","bol"=>"53","lucy"=>"24","jone"=>"78");
var_dump(arsort($eee));
var_dump($eee);

$eee1=[3,65,237,78,9,23,9,3,4,];
arsort($eee1);
var_dump($eee1);


// 对关联数组按照键值进行升序排序
$eee2=[3,65,237,78,9,23,9,3,4,];
asort($eee2);
var_dump($eee2);


// 返回数组中元素的数目,从1开始
$f=[3,65,237,78,9,23,9,3,4,];
var_dump(count($f));


// 返回数组中的当前元素。 
// 每个数组中都有一个内部的指针指向它的"当前"元素，初始指向插入到数组中的第一个元素。
$ff=array("tim"=>"46","bol"=>"53","lucy"=>"24","jone"=>"78");
echo current($ff) ;



// 检查数组中是否存在指定的值
echo "<br>";
$fff=[3,65,237,78,9,23,9,3,4,];
if (in_array("3", $fff)) {
	echo "匹配成功";
}else{
	echo "匹配失败";
};



// 从关联数组返回数组内部指针当前指向元素的键名
$g=array("tim"=>"46","bol"=>"53","lucy"=>"24","jone"=>"78");
echo key($g);


// 对数组按照键名逆向排序,改变原来的数组
$gg=array("2"=>"tim","6"=>"53","65"=>"24","5"=>"kk");
krsort($gg);
var_dump($gg);

// 对数组按照键名排序,改变原来数组
$ggg=array("2"=>"tim","6"=>"53","65"=>"24","5"=>"kk","7"=>"last2");
ksort($ggg);
var_dump($ggg);


// 对数组逆向排序，改变原来数组
$h=["aa","bb","cc","dd","ee"];
rsort($h);
var_dump($h);


// 将数组打乱
$hh=["aa","bb","cc","dd","ee"];
shuffle($hh);
var_dump($hh);



// 对数组排序,改变原来的数组
$arr =array();
$arr[0] = "George";
$arr[1] = "John";
$arr[2] = "Thomas";
$arr[3] = "James";
$arr[4] = "Adrew";
$arr[5] = "Martin";
sort($arr);
var_dump($arr);


 ?>